package com.goldstone.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.IndexColumn;


@Entity
@Table(name = "PRODUCT")
public class Product extends AuditableEntity {

	private String cref;
	private String sref;
	private String name;
	private String enName;
	private Attachment logo;
	private String ref;
	private List<ProDesc> descs = new ArrayList<ProDesc>();
	
	private String status;
	private Boolean deleted = false;
	//private Integer version;
	private List<Color> colors = new ArrayList<Color>();
	private List<Size> sizes = new ArrayList<Size>();
	private List<PriceLog> priceLogs = new ArrayList<PriceLog>();
	private List<Attachment> pics = new ArrayList<Attachment>();
	private List<ProComment> comments = new ArrayList<ProComment>();
	private List<Company> suppliers = new ArrayList<Company>();
	private List<Company> customers = new ArrayList<Company>();
	private List<Tag> tags = new ArrayList<Tag>();
	private Integer proNum;
	
	private List<String> suppliersId;
	private List<String> customersId;
	
	public Product() {}
	
	public Product(String ref, String name, String enName, String... des) {
		this.ref = ref;
		this.enName = enName;
		this.name = name;
		int length = des.length;
		if(length % 2 == 1) 
			length--;
		for(int i = 0; i < length; i += 2) {
			descs.add(new ProDesc(des[i], des[i + 1]));
		}
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getCref() {
		return cref;
	}

	public void setCref(String cref) {
		this.cref = cref;
	}

	public String getSref() {
		return sref;
	}

	public void setSref(String sref) {
		this.sref = sref;
	}

	public String getEnName() {
		return enName;
	}

	public void setEnName(String enName) {
		this.enName = enName;
	}

	public String getRef() {
		return ref;
	}
	public void setRef(String ref) {
		this.ref = ref;
	}
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinColumn(name = "P_ID")
	public List<ProDesc> getDescs() {
		return descs;
	}

	public void setDescs(List<ProDesc> descs) {
		this.descs = descs;
	}

	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
//	@Version
//	public Integer getVersion() {
//		return version;
//	}
//	public void setVersion(Integer version) {
//		this.version = version;
//	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
	@ManyToMany
	@JoinTable(
		name = "PRODUCT_COLOR",
		joinColumns = @JoinColumn(name = "pro_id"),
		inverseJoinColumns = @JoinColumn( name = "color_id") 
	)
	public List<Color> getColors() {
		return colors;
	}
	public void setColors(List<Color> colors) {
		this.colors = colors;
	}
	
	@ManyToMany
	@JoinTable(
		name = "PRODUCT_SIZE",
		joinColumns = @JoinColumn(name = "pro_id"),
		inverseJoinColumns = @JoinColumn( name = "size_id") 
	)
	public List<Size> getSizes() {
		return sizes;
	}
	public void setSizes(List<Size> sizes) {
		this.sizes = sizes;
	}
	@ElementCollection
	@JoinTable(name = "PRICE_LOG", joinColumns = {@JoinColumn(name = "PRO_ID")})
	@IndexColumn(name = "linenum")
	public List<PriceLog> getPriceLogs() {
		return priceLogs;
	}
	public void setPriceLogs(List<PriceLog> priceLogs) {
		this.priceLogs = priceLogs;
	}
	
	public void addDesc(ProDesc prodesc) {
		this.descs.add(prodesc);
	}
	
	public void addColor(Color color) {
		this.colors.add(color);
	}
	
	public void addSize(Size size) {
		this.sizes.add(size);
	}
	
	public void addProComment(ProComment comm) {
		this.comments.add(comm);
	}
	
	public void addProComment(String comm) {
		this.comments.add(new ProComment(comm));
	}

	public void addPrice(PriceLog priceLog) {
		this.priceLogs.add(priceLog);
	}
	public Boolean getDeleted() {
		return deleted == null ? false : true;
	}

	public void setDeleted(Boolean deleted) {
		this.deleted = deleted;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(
		name = "PRODUCT_IMAGES",
		joinColumns = @JoinColumn(name = "pro_id"),
		inverseJoinColumns = @JoinColumn( name = "atta_id") 
	)
	public List<Attachment> getPics() {
		return pics;
	}

	public void setPics(List<Attachment> pics) {
		this.pics = pics;
	}
	@OneToOne
	@JoinColumn(name = "logo")
	public Attachment getLogo() {
		return logo;
	}
	
	public void setLogo(Attachment logo) {
		this.logo = logo;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinColumn(name = "P_ID")
	public List<ProComment> getComments() {
		return comments;
	}

	public void setComments(List<ProComment> comments) {
		this.comments = comments;
	}
	
	@Transient
	public String getTagsString() {
		String s = "";
		for(Tag tag : getTags()) {
			s += tag.getName() + ",";
		}
		return s;
	}

	@ManyToMany
	@JoinTable(
		name = "PRODUCT_TAGS",
		joinColumns = @JoinColumn(name = "pro_id"),
		inverseJoinColumns = @JoinColumn( name = "tag_id") 
	)
	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}
	
	@Transient
	public String getTagString() {
		String s = "";
		for(Tag tag : tags) {
			s += tag.getName() + ",";
		}
		return s;
	}

	public Integer getProNum() {
		return proNum;
	}

	public void setProNum(Integer proNum) {
		this.proNum = proNum;
	}
	@ManyToMany
	@JoinTable(
		name = "PRODUCT_SUPPLIER",
		joinColumns = @JoinColumn(name = "pro_id"),
		inverseJoinColumns = @JoinColumn( name = "supp_id") 
	)
	public List<Company> getSuppliers() {
		return suppliers;
	}

	public void setSuppliers(List<Company> suppliers) {
		this.suppliers = suppliers;
	}
	@ManyToMany
	@JoinTable(
		name = "PRODUCT_CUSTOMER",
		joinColumns = @JoinColumn(name = "pro_id"),
		inverseJoinColumns = @JoinColumn( name = "cust_id") 
	)
	public List<Company> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Company> customers) {
		this.customers = customers;
	}

	@Transient
	public List<String> getSuppliersId() {
		return suppliersId;
	}

	public void setSuppliersId(List<String> suppliersId) {
		this.suppliersId = suppliersId;
	}

	@Transient
	public List<String> getCustomersId() {
		return customersId;
	}

	public void setCustomersId(List<String> customersId) {
		this.customersId = customersId;
	}
	
}
